package MyFramePro.Service.Impl;

import MyFramePro.Constants.SystemConstants;
import MyFramePro.Domain.Entity.Category;
import MyFramePro.Domain.Entity.Link;
import MyFramePro.Domain.ResponseResult;
import MyFramePro.Domain.Vo.LinkVo;
import MyFramePro.Domain.Vo.PageVo;
import MyFramePro.Mapper.LinkMapper;
import MyFramePro.Service.LinkService;
import MyFramePro.Utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2023-04-30 23:03:10
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Resource
    private LinkServiceImpl linkService;

    /**
     * 查询友链的功能
     */
    @Override
    public ResponseResult getAllLink() {
        //1.查询审核通过的友链信息
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_PASS);
        List<Link> linkList = linkService.list(queryWrapper);

        //2.进行封装成 Vo 类型的对象
        List<LinkVo> linkVos = BeanCopyUtils.myCopyBeanList(linkList, LinkVo.class);

        return ResponseResult.okResult(linkList);
    }



    /**
     * 这里是友链的分页查询的功能
     */
    @Override
    public ResponseResult selectLinkPageList(Integer pageNum, Integer pageSize, String name, String status) {

        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        //1.条件过滤
        queryWrapper.like(StringUtils.hasText(name),Link::getName,name);
        if(!StringUtils.hasText(status)){
            queryWrapper.eq(Link::getStatus,SystemConstants.CATEGORY_STATUS_NORMAL);    //若没有传入状态，则默认查询状态为正常的
        }else {
            queryWrapper.eq(Link::getStatus,status);
        }
        List<Link> linkList = linkService.list(queryWrapper);

        //2.由于前端需求字段，这里进行 Vo 类型的封装
        List<LinkVo> linkVos = BeanCopyUtils.myCopyBeanList(linkList, LinkVo.class);

        //3.分页查询
        Page<Link> page = new Page<>(pageNum,pageSize);
        linkService.page(page,queryWrapper);

        //4.再次封装为 Vo 类型
        PageVo pageVo = new PageVo(linkVos, page.getTotal());

        return ResponseResult.okResult(pageVo);
    }


    /**
     * 这里为新增友链的功能
     */
    @Override
    public ResponseResult addLinks(LinkVo linkVo) {

        Link link = BeanCopyUtils.myCopyBean(linkVo, Link.class);
        linkService.save(link);

        return ResponseResult.okResult();
    }


    /**
     * 修改友链————查询友链信息功能
     */
    @Override
    public ResponseResult selectLinkInfo(Long id) {

        Link link = linkService.getById(id);
        LinkVo linkVo = BeanCopyUtils.myCopyBean(link, LinkVo.class);

        return ResponseResult.okResult(linkVo);
    }


    /**
     * 这里是修改友链的功能
     */
    @Override
    public ResponseResult updateLink(LinkVo linkVo) {

        Link link = BeanCopyUtils.myCopyBean(linkVo, Link.class);
        linkService.updateById(link);

        return ResponseResult.okResult();
    }


    /**
     * 这里是删除友链的功能
     */
    @Override
    public ResponseResult deleteLink(Long id) {

        linkService.removeById(id);

        return ResponseResult.okResult();
    }


    /**
     * 这里是更改审核状态的功能
     */
    @Override
    public ResponseResult changeLinkStatus(LinkVo linkVo) {

        Link link = BeanCopyUtils.myCopyBean(linkVo, Link.class);
        linkService.updateById(link);

        return ResponseResult.okResult();
    }


}

